/****************************************************************************/
/*                                                                          */
/*   FILE:    Session.h	                                                    */
/*   DATE:    30/06/97                                                      */
/*   PURPOSE: Header File for DVB-CI Session layer                        	*/
/*   TAB STOPS : 4															*/
/*   HISTORY:				                                    			*/
/*   Version    Date	  	    Written by	     Notes	    	    		*/
/*	 1.0	    30/06/1997      SCM              Initial implementation 	*/
/*                                                                    	    */
/*--------------------------------------------------------------------------*/
/*   GENERAL COMMENTS                                                 	    */
/*									    									*/
/****************************************************************************/
#ifndef __SESSION_H
#define __SESSION_H

#include "xmsg.h"
#include "sm_types.h"

#ifdef __cplusplus
extern "C" {
#endif

/*--------------------------------------------------------------------------*/
/* Symbol Definitions                                                       */
/*--------------------------------------------------------------------------*/
/* states of transport state diagram */
#define SESS_NOT_USED	1
#define SESS_IDLE 		2
#define SESS_OPEN		3
#define SESS_ACTIVE		4
#define SESS_CREATION	5
#define SESS_CLOSE		6  
#define SESS_RELEASE	7
#define SESS_DELETION	8

#define GETSESSSTATNAME(transtat) ((transtat)==SESS_NOT_USED?"SESS_NOT_USED":((transtat)==SESS_IDLE?"SESS_IDLE":((transtat)==SESS_OPEN?"SESS_OPEN":	((transtat)==SESS_ACTIVE?"SESS_ACTIVE":	((transtat)==SESS_CREATION?"SESS_CREATION":((transtat)==SESS_CLOSE?"SESS_CLOSE":((transtat)==SESS_RELEASE?"SESS_RELEASE":((transtat)==SESS_DELETION?"SESS_DELETION":"UNKN_SESSSTAT"))))))))

/*--------------------------------------------------------------------------*/
/* Structures Definitions                                                   */
/*--------------------------------------------------------------------------*/
typedef struct
{
	unsigned char  m_state;			/* state of session connection			*/
	unsigned char  m_tcid;			/* transport connection number			*/
	unsigned short m_ssnb;			/* session number on tcid				*/
	unsigned char  m_rtcid;			/* tcid for routing sessions			*/
	unsigned short m_rssnb;			/* session number on routing tcid		*/
	unsigned short m_timerId;			/* timeout timer identifier				*/
	unsigned long  m_ressId; 		/* resource identifier				*/
}sess_t;		/* informations for one session connection					*/
	
/*--------------------------------------------------------------------------*/
/* Function Prototypes                                                      */
/*--------------------------------------------------------------------------*/
int session(void* param);				/* Entry point of SESS Task					*/

int  SESS_Init(void);			/* Module initialization					*/
void SESS_State(xmsg_t* pXmsg);	        /* Entry point of the Session State Diagram Module */
void SESS_End(void);			/* Ending module 							*/

int  SESS_Spdu(xmsg_t* pXmsg);			/* SPDU received from TRANS Task			*/
int  SESS_Apdu(xmsg_t* pXmsg);			/* APDU received from RM Task				*/
int  SESS_Created(xmsg_t* pXmsg);		/* SS_TC_CREATED received from TRANS Task	*/
int  SESS_Deleted(xmsg_t* pXmsg);		/* SS_TC_DELETED received from TRANS Task	*/
int  SESS_RMDelete(xmsg_t* pXmsg);		/* RM_DELETE_TC received from RM Task		*/
int  SESS_OpenRsp(xmsg_t* pXmsg);		/* RM_OPEN_SS_RSP received from RM Task		*/
int  SESS_RMClose(xmsg_t* pXmsg);		/* RM_CLOSE_SS received from RM Task		*/
int  SESS_Timeout(xmsg_t* pXmsg);		/* TIMEOUT received							*/
int SESS_OpenReq(xmsg_t* pXmsg); // add by pmy
              
int  SESS_AllocSsnbIndex(unsigned char tcid);		/* get a new entry in g_ses_tab_ssnb*/
int  SESS_FreeSsnbIndex(int ssnbIndex); /* delete an entry in g_ses_tab_ssnb				*/
int  SESS_GetSsnbIndex(unsigned  short ssnb);	/* get the ssnbIndex in g_ses_tab_ssnb	*/
int  SESS_RoutSsnb(unsigned short ssnb, unsigned char rtcid);	/* get a new session number for the routing tcid 			 */
int  TRPT_GetSsnbIndexByTM(unsigned  short timerId);/* get the ssnbIndex in g_ses_tab_ssnb according to a TIMEOUT timer */
unsigned short SESS_SetTimer(unsigned short value); /* set a TIMESESS timer*/
void SESS_Kill_Timer(int ssnbIndex);/* kill a pending TIMEOUT timer				*/

/*--------------------------------------------------------------------------*/
/* State Diagram                                                      */
/*--------------------------------------------------------------------------*/
void SESS_Action(int ssnbIndex, xmsg_t* pXmsg);		/* Entry point  	*/
int SESS_Idle(int ssnbIndex, xmsg_t* pXmsg);		/* SESS_IDLE state		*/
int SESS_Open(int ssnbIndex, xmsg_t* pXmsg);		/* SESS_OPEN state		*/
int SESS_Active(int ssnbIndex, xmsg_t* pXmsg);		/* SESS_ACTIVE state		*/
int SESS_Creation(int ssnbIndex, xmsg_t* pXmsg);	/* SESS_CREATION state	*/
int SESS_Release(int ssnbIndex, xmsg_t* pXmsg);		/* SESS_RELEASE state	*/
int SESS_Deletion(int ssnbIndex, xmsg_t* pXmsg);	/* SESS_DELETION state	*/
int SESS_Close(int ssnbIndex, xmsg_t* pXmsg);		/* SESS_CLOSE state		*/

int SESS_TCCreated(unsigned char tcid);							/* send a RM_TC_CREATED message		*/
int SESS_TCDeleted(unsigned char tcid);							/* send a RM_TC_DELETED message		*/
int SESS_DeleteTC(unsigned char tcid);							/* send a SS_DELETE_TC message		*/
void SESS_NotOpen(xmsg_t* pXmsg);			/* send a negative open_session_response */



/*--------------------------------------------------------------------------*/
/* SPDU code/decode
/*--------------------------------------------------------------------------*/

/* SPDU coding */
int SESS_Code(unsigned char tag, unsigned short ssnb, xpdu_t *pXpdu, unsigned char *pbSpdu);

/* SPDU decoding */
int SESS_Decode(unsigned char *pbSpdu, unsigned long len, unsigned char *tag, unsigned short *ssnb, xpdu_t *pXpdu);

BOOL SESS_XmsgDecode(xmsg_t* pXmsg, unsigned char *pbSpduTag, 
					unsigned short* pSsnb, xpdu_t *pXpdu);

/* SSNB coding */
void SESS_CodSsnb( unsigned short ssnb, unsigned char *pbSpdu);

/* SSNB decoding */
void SESS_DecodSsnb(unsigned char *pbSpdu, unsigned short *ssnb);

#ifdef __cplusplus
}
#endif


#endif	/* !__SESSION_H */
